package com.leyou.item.controller;

import com.leyou.item.pojo.*;
import com.leyou.item.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyEmitter;


import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("category")
public class CategoryController {

    @Autowired
    private CategoryService service;

    /**
     * 根据parentId查询标签
     * @param pid
     * @return
     */
    @RequestMapping("list")
    public ResponseEntity<List<Category>> queryCategoryListByParentId(@RequestParam(value="pid" ,defaultValue = "0") Long pid){

        try{

            if(pid == null || pid < 0){
                //pid为空或小于0返回400
                return ResponseEntity.badRequest().build();
            }
            List<Category> categories = this.service.queryCategoryListByParentId(pid);
            if(CollectionUtils.isEmpty(categories)){
                //结果为空返回404
                return ResponseEntity.notFound().build();
            }
            //正常返回,响应200
            return ResponseEntity.ok(categories);
        }catch (Exception e){
            e.printStackTrace();
        }
        //出错,响应500
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();

    }

    @GetMapping("bid/{bid}")
    public ResponseEntity<List<Category>> queryByBrandId(@PathVariable("bid")Long bid){
        List<Category> categories = service.queryByBrandId(bid);
        if (categories == null || categories.size()==0){
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        return ResponseEntity.ok(categories);
    }

    @GetMapping("names")
    public ResponseEntity<List<String>> queryNameById(@RequestParam("ids")List<Long> ids){
        List<String> list = this.service.queryNamesByIds(ids);
        if(list == null || list.size()==0){
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        return ResponseEntity.status(HttpStatus.OK).body(list);
    }

    @GetMapping("all/level")
    public ResponseEntity<List<Category>> queryAllCategoryByCid3(@RequestParam("id")Long id){
        List<Category> categories = this.service.queryAllCategoryByCid3(id);
        if(categories==null){
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        return ResponseEntity.ok(categories);
    }

    @GetMapping("provin")
    @ResponseBody
    public ResponseEntity<List<Province>> queryProvin(){
        List<Province> provinces = service.queryProvin();
        return ResponseEntity.ok(provinces);
    }

    @GetMapping("city")
    @ResponseBody
    public ResponseEntity<List<City>> queryCity(){
        List<City> cities = service.queryCity();
        return ResponseEntity.ok(cities);
    }

    @GetMapping("area")
    @ResponseBody
    public ResponseEntity<List<Area>> queryArea(){
        List<Area> areas= service.queryArea();
        return ResponseEntity.ok(areas);
    }

    /**
     * 根据Id查询分类
     *
     * @param id
     * @return
     */
    @GetMapping
    public ResponseEntity<Category> queryCategoryById(@RequestParam("id") Long id) {

        Category category = this.service.queryCategoryById(id);

        if (category == null) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(category);
    }

    /********************************第一个需求start*****************************************/
    @RequestMapping("categoryList")
    public ResponseEntity<List<Cate>> queryCategoryList(@RequestParam("pid") Long pid){
        try{

            if(pid == null || pid < 0){
                //pid为空或小于0返回400
                return ResponseEntity.badRequest().build();
            }
            List<Cate> categories = this.service.queryCategoryListByPid(pid);
            if(CollectionUtils.isEmpty(categories)){
                //结果为空返回404
                return ResponseEntity.notFound().build();
            }
            //正常返回,响应200
            return ResponseEntity.ok(categories);
        }catch (Exception e){
            e.printStackTrace();
        }
        //出错,响应500
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

}
